package com.atguigu.gmall.gateway.filter;

import com.atguigu.gmall.common.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Gateway 网关过滤器-全局过滤器:针对于所有路由生效
 */
@Slf4j
@Component
public class AccessFilter implements GlobalFilter, Ordered {
    /**
     * 过滤器逻辑
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String ipUrl = IpUtil.getGatwayIpAddress(exchange.getRequest());
        log.info("AccessFilter 网关服务被访问"+ipUrl);

        return chain.filter(exchange);
    }

    /**
     * 过滤器执行顺序,,越小,越先执行
     * @return
     */

    @Override
    public int getOrder() {
        return 0;
    }
}
